package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.UserInfoMapper;
import com.tulele.vtool.mapper.WantedUsersMapper;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.model.logEntity.WantedUsers;
import com.tulele.vtool.model.logEntity.WantedUsersLog;
import com.tulele.vtool.service.WantedUsersService;
import com.tulele.vtool.utils.SimilarImageSearch;
import com.tulele.vtool.utils.SysConstant;
import com.tulele.vtool.utils.ToolUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service("wantedUsersService")
public class WantedUsersServiceImpl implements WantedUsersService {
    @Autowired
    private WantedUsersMapper wantedUsersMapper;


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List<WantedUsers> queryByNickName(String nickName) {
        return wantedUsersMapper.queryByNickName(nickName);
    }

    @Override
    public boolean checkAvatar(Integer targetUserId, String nickName) {
        try {
            List<WantedUsers> wantedUsers = wantedUsersMapper.queryByNickName(nickName);

            if (wantedUsers != null && wantedUsers.size() > 0) {
                UserInfo targetUser = userInfoMapper.queryUserMainInfo(targetUserId);

                if (targetUser != null && targetUser.getAvatarUrl() != null) {

                    String tarAvatarName = UUID.randomUUID().toString() + ".jpg";

                    ToolUtil.downloadAvatarPic(targetUser.getAvatarUrl(), targetUser.getUserId(), tarAvatarName, SysConstant.WX_AVATAR_DOWNLOAD_PATH2);

                    String tarAvatarPath = SysConstant.WX_AVATAR_DOWNLOAD_PATH2 + targetUser.getUserId() + "/" + tarAvatarName;
                    String tarHashCode = SimilarImageSearch.produceFingerPrint(tarAvatarPath);

                    for (WantedUsers wu : wantedUsers) {
                        String avatarName = UUID.randomUUID().toString() + ".jpg";
                        ToolUtil.downloadAvatarPic(wu.getAvatarUrl(), wu.getUserId(), avatarName, SysConstant.WX_AVATAR_DOWNLOAD_PATH2);
                        String avatarPath = SysConstant.WX_AVATAR_DOWNLOAD_PATH2 + wu.getUserId() + "/" + avatarName;
                        String hashCode = SimilarImageSearch.produceFingerPrint(avatarPath);

                        int difference = SimilarImageSearch.hammingDistance(tarHashCode, hashCode);
                        FileUtils.forceDeleteOnExit(new File(avatarPath));
                        if (difference == 0) {
                            WantedUsersLog wantedUsersLog = new WantedUsersLog();
                            wantedUsersLog.setUserId(targetUser.getUserId());
                            wantedUsersLog.setNickName(targetUser.getNickName());
                            wantedUsersLog.setAvatarUrl(targetUser.getAvatarUrl());
                            wantedUsersLog.setGzhCode(targetUser.getGzhCode());
                            wantedUsersLog.setCreateTime(new Date());

                            wantedUsersMapper.addWantedUsersLog(wantedUsersLog);
                            FileUtils.forceDeleteOnExit(new File(tarAvatarPath));
                            return true;
                        }
                    }
                    FileUtils.forceDeleteOnExit(new File(tarAvatarPath));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}